package com.gqc.controller;




import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gqc.bean.Cart;
import com.gqc.bean.Order;
import com.gqc.bean.User;
import com.gqc.service.CartService;
import com.gqc.service.OrderService;
import com.gqc.service.UserService;

@Controller
public class UserController {
	@Autowired
	UserService userService;
	@Autowired
	CartService cartService;
	@Autowired
	OrderService orderService;
	@ResponseBody
	@RequestMapping("/login")
	public String login(String user,String password,HttpSession session) {
	
	
		String status = null;
		User user2 = userService.getByLoginName(user);
		if (user2 != null && user2.getPassword().equals(password)) {
			if (user2.getIdentity().equals("user")) {
				String identity="business"; 
				List<User> list=userService.queryByIdentity(identity);
				List<Cart> list3 = cartService.queryCart(user2.getId());
				List<Order> list4 = orderService.findOrderByUser(user2.getId());

				session.setAttribute("list1",list);
				 session.setAttribute("list3", list3);
				 session.setAttribute("list3size",list3.size());
				 session.setAttribute("list4", list4);

				status = "user";
			}else if (user2.getIdentity().equals("business")) {
				status = "business";
			}
	
			session.setAttribute("user", user2);
		} else {
			status = "账户名或密码不存在";
		}
		return status;

		
	}
	
	
	@RequestMapping("/register")
	public String  register( User user,HttpSession session){
		boolean status = false;
		String sta = null;
		try {
			status=userService.register(user);
		
		} catch (Exception e) {
			status = false;
			return "error";
		}
		User user2 = userService.getByLoginName(user.getLoginname());
		session.setAttribute("user", user2);
		if (user2.getIdentity().equals("user")) {
			sta = "user";
		}else if (user2.getIdentity().equals("business")) {
			sta = "business";
		}
		return sta;
		
     }
//	@RequestMapping("/checkLoginName")
//	@ResponseBody
//	public String checkLoginName(@RequestBody User user) {
//		Boolean status = userService.checkLoginname(user);
//		System.out.println("check: " + status);
//		return status.toString();
//	}
	@RequestMapping("checkName")
	@ResponseBody//返回给jsp字符串格式
	public String checkName(String username) {
		//根据用户名查询是否存在该用户名
		boolean user=userService.checkLoginname(username);
		
		//当对象不为空，说明用户名存在
		
		return "{\"msg\":\""+user+"\"}";
	}
	@RequestMapping("/logout")
	@ResponseBody
	public String logOut(HttpSession session) {
		session.removeAttribute("user");
		return "login";
	}
	@RequestMapping(value = "/updateUser")
	public String update(@RequestParam("id")int id,@RequestParam("loginname") String loginname,@RequestParam("password")String password,@RequestParam("name")String name,@RequestParam("tel")String tel,@RequestParam("address")String address,@RequestParam("identity")String identity,HttpSession session) {
		User user = new User();
		user.setId(id);
		user.setAddress(address);
		user.setIdentity(identity);
		user.setLoginname(loginname);
		user.setTel(tel);
		user.setPassword(password);
		user.setName(name);
		System.out.println(user.toString());
		
		Boolean status = userService.updateAdmin(user);
		String identity01="business";
		User user2 = userService.getByLoginName(loginname);
		List<User> list=userService.queryByIdentity(identity01);
		List<Cart> list3 = cartService.queryCart(user2.getId());
		List<Order> list4 = orderService.findOrderByUser(user2.getId());
		session.setAttribute("list1",list);
		 session.setAttribute("list3", list3);
		 session.setAttribute("list3size",list3.size());
		 session.setAttribute("list4", list4);
		 session.setAttribute("user", user2);
		if (user2.getIdentity().equals("business")) {
			return "business";
		}else if (user2.getIdentity().equals("user")) {
			return "user";
		}
		return status.toString() ;
	}

	@RequestMapping("/queryIdentity")
	public String HelloSpring(HttpSession session) {
		
		 String identity="business"; 
		 List<User> list=userService.queryByIdentity(identity);
		 session.setAttribute("list",list);
		
		  return "showBusiness";
		 
	}

}
